package com.atguigu.springbootmybatisplusrabbitmq;

import com.atguigu.springbootmybatisplusrabbitmq.entity.Product;
import com.atguigu.springbootmybatisplusrabbitmq.mapper.ProductMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
public class testProductConcurrent {
    @Resource
    private ProductMapper productMapper;

    @Test
    public void testSelectProducts(){
        Page<Product> page = new Page<>();
        List<Product> products = productMapper.selectList(null);
        products.forEach(System.out::println);
    }

    @Test
    public void testConcurrentUpdata(){
        //小李取数据
        Product p1=productMapper.selectById(1l);
        //小王取数据
        Product p2=productMapper.selectById(1l);
        //小李修改+50
        p1.setPrice(p1.getPrice()+50);
        int i = productMapper.updateById(p1);
        System.out.println("小李修改价格成功:"+i);
        //小王修改-30
        p2.setPrice(p2.getPrice()-30);
        int res2 = productMapper.updateById(p2);
        System.out.println("小王修改价格成功:"+res2);
        if(res2==0){
            p2 = productMapper.selectById(1l);
            p2.setPrice(p2.getPrice()-30);
            res2=productMapper.updateById(p2);
        }
        System.out.println("小王修改的结果："+res2);
        //老板看结果

        Integer result=productMapper.selectById(1l).getPrice();
        System.out.println("老板看到的价格："+result);
    }
}
